package com.letsrpg.wallhaven.provider;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by seonfly on 2016/3/20.
 */
public class ImageUrls {

    public static List<String> getImageUrls(int pageNum) {
        List<String> result = new ArrayList<>();
        try {
            String IMAGE_URL = "http://alpha.wallhaven.cc/latest?page=";
            Document document = Jsoup.connect(IMAGE_URL + pageNum).get();
            Elements elements = document.getElementsByTag("figure");
            for (final Element e : elements.subList(0, elements.size() - 1)) {
                result.add(e.attr("data-wallpaper-id"));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return result;
    }


    public static String getImageUrl(String id) {
        String IMAGE_URL_FORMAT = "http://wallpapers.wallhaven.cc/wallpapers/full/wallhaven-%s.jpg";
        return String.format(IMAGE_URL_FORMAT, id);
    }

    public static String getThumbImageUrl(String id) {
        String IMAGE_THUMB_URL_FORMAT = "http://alpha.wallhaven.cc/wallpapers/thumb/small/th-%s.jpg";
        return String.format(IMAGE_THUMB_URL_FORMAT, id);
    }

}
